import * as vscode from 'vscode';

class MessagebyTime {
    intervalId:  NodeJS.Timeout | null = null;
    closeCondition: boolean = false;
    constructor (header: string, msg: string, total: number = 100, setup: number = 10){
        vscode.window.withProgress(
            {
                location: vscode.ProgressLocation.Notification,
                title: header,
                cancellable: false
            },
            (progress, token) => {
                return new Promise<void>((resolve) => {
                    let completed = 0;
                    progress.report({ increment: 1, message: `${msg}` });
                    this.intervalId = setInterval(() => {
                        completed += setup;
                        if(this.closeCondition){
                            completed = total;
                        }
                        progress.report({ increment: 100 / ((total / setup)-1), message: `${msg}` });
                        if (completed >= total ) {
                            if( this.intervalId  !== null){
                                clearInterval( this.intervalId ); 
                                resolve();                             
                            }
                        }
                    }, 1000);
                    token.onCancellationRequested(() => {
                        if( this.intervalId  !== null){
                            clearInterval( this.intervalId );
                        }
                        resolve();
                    });
                });
            }
        );
    }
    close(){
        this.closeCondition = true;
    }
}

export  { MessagebyTime };